package ${packageName};

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.allways.dubbo.mdm.beans.DataChangeLog;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.exception.MdmExceptionCode;
import com.allways.dubbo.mdm.service.BaseDataUtils;
import com.allways.dubbo.mdm.service.log.IDataChangeLogService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import com.allways.dubbo.base.dao.MdmBaseDaoImpl;
import ${EntityClass};
import ${serviceInterFace};

/**
 * <p>ClassName: I${UpperKey}Service</p>
 * <p>Description: ${ZhKey}Service接口</p>
 * <p>Author: ${author}</p>
 * <p>Date: ${createDate}</p>
 */
@Service("mdm${UpperKey}Service")
public class ${UpperKey}ServiceImpl extends MdmBaseDaoImpl implements I${UpperKey}Service {
	Log log = LogFactory.getLog(${UpperKey}ServiceImpl.class);

    @Autowired
    BaseDataUtils baseDataUtils;
    
     @Autowired
    IDataChangeLogService dataChangeLogService;
    
    @Override
    public int add${UpperKey}(${UpperKey} ${key}) {
   		log.debug("For the new ${key} parameters:"+ ${key}.toJson());
   		validColumnLen(${key});
    	
        //无code的不能插入
        if(StringUtils.isEmpty(${key}.get${UpperKey}Code())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code重复的不允许插入
        if(null != get${UpperKey}ByCode(${key}.get${UpperKey}Code())){
            throw new MdmException(MdmExceptionCode.CODE_REPEATED,null);
        }
        
        int cnt=insert("${key}.insert${UpperKey}",${key});
        
        /*数据变更日志*/
        DataChangeLog datalog = new DataChangeLog();
        datalog.setDataType(DataChangeLog.DataType.${key}.getName());
        datalog.setDataPk(${key}.get${UpperKey}Id().toString());
        datalog.setModifyType(DataChangeLog.OPER_TYPE_ADD);
        dataChangeLogService.addDataChangeLog(datalog);
        
        return cnt;
    }

    @Override
    public int update${UpperKey}(${UpperKey} ${key}) {
    	log.debug("Update for the ${key} parameters:"+ ${key}.toJson());
    	validColumnLen(${key});
     	//无code的不能更新
        if(StringUtils.isEmpty(${key}.get${UpperKey}Code())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        //code记录不存在
        ${UpperKey} old${UpperKey} = get${UpperKey}ByCode(${key}.get${UpperKey}Code());
        if(null == old${UpperKey}){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_RESULT,null);
        }
        <#list fieldList as field>
		old${UpperKey}.set${field.upperName}(${key}.get${field.upperName}());
		</#list> 
        
        int cnt= update("${key}.update${UpperKey}",old${UpperKey});
        /*数据变更日志*/
        DataChangeLog datalog = new DataChangeLog();
        datalog.setDataType(DataChangeLog.DataType.${UpperKey}.getName());
        datalog.setDataPk(old${UpperKey}.get${UpperKey}Id().toString());
        datalog.setModifyType(DataChangeLog.OPER_TYPE_EDIT);
        dataChangeLogService.addDataChangeLog(datalog);
        
        return cnt;
    }

    @Override
    public int delete${UpperKey}(${UpperKey} ${key}) {
    	log.debug("Delete for the ${key} parameters:"+ ${key}.toJson());
    	 
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(${key}.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
     	//无code的不能删除
        if(StringUtils.isEmpty(${key}.get${UpperKey}Code())){
            throw new MdmException(MdmExceptionCode.CODE_IS_EMPTY,null);
        }
        ${UpperKey} full${UpperKey} = get${UpperKey}ByCode(${key}.get${UpperKey}Code());
        
        /* 数据变更日志 */
        DataChangeLog datalog = new DataChangeLog();
        datalog.setDataType(DataChangeLog.DataType.${UpperKey}.getName());
        datalog.setDataPk(full${UpperKey}.get${UpperKey}Id().toString());
        datalog.setModifyType(DataChangeLog.OPER_TYPE_DELETE);
        dataChangeLogService.addDataChangeLog(datalog);
        
        return delete("${key}.delete${UpperKey}",${key});
    }

    @Override
    public ${UpperKey} get${UpperKey}ByCode(String code) {
    	log.debug("Query for ${key} code:" + code);
        return queryForObject("${key}.get${UpperKey}ByCode",code);
    }
    @Override
    public long get${UpperKey}Count(${UpperKey} ${key}) {
    	log.debug("Query for ${key} count parameters:" + ${key}.toJson());
        return queryForObject("${key}.get${UpperKey}Count",${key});
    }
   @Override
    public List<${UpperKey}> query${UpperKey}s(${UpperKey} ${key}, int firstRow, int lastRow) {
      	log.debug("Query for ${key} parameters:" + ${key}.toJson()+"\r\n,Page parameters:["+firstRow+","+lastRow+"]");
        if(firstRow<0 || firstRow>lastRow || lastRow<0){
            throw new RuntimeException("Digital is not logical");
        }
        if(lastRow-firstRow > MAX_RETURN_NUM){
            lastRow = firstRow + MAX_RETURN_NUM;
        }
        Map<String ,Object> param = new HashMap<String,Object>();
        param.put("${key}",${key});
        param.put("firstRow",firstRow);
        param.put("rowSize",lastRow-firstRow);
        return queryList("${key}.query${UpperKey}s", param);
    }
    
    /**
     * 验证字段长度
     * @param ${key} 需要验证的对象
     */
    private void validColumnLen(${UpperKey} ${key}){
    	<#list noCommonFieldList as field>
		if(!StringUtils.isEmpty(${key}.get${field.upperName}()) && ${key}.get${field.upperName}().length()>${field.length}){
            throw new MdmException(MdmExceptionCode.LENGTH_OUT_OF_RANGE,new Object[]{${field.length}},null);
        }
		</#list> 
        
        //数据来源不明的不能操作
        if(!baseDataUtils.isDataSourceAvailable(${key}.getDataSource())){
            throw new MdmException(MdmExceptionCode.UNKNOWNED_DATASOURCE,null);
        }
    }
    
}
